package com.base.controller;

import com.base.po.voip.Users;
import com.base.service.UsersService;
import com.base.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class content_oem {
    @Autowired
    private UsersService usersService;

    @RequestMapping("/content_oem")
    public ModelAndView oem(HttpServletRequest request, String datetime1, String datetime2) {
        //如果传入的两个时间中有空值，则选择前后一个月内的OEM新增量的数据
        if (datetime1 == null || datetime2 == null || datetime1.equals("null") || datetime2.equals("null")) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            //获取当前月第一天：
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
            String first = format.format(c.getTime());
            datetime1 = first;

            //获取当前月最后一天
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
            String last = format.format(ca.getTime());
            datetime2 = last;
        }
        List<Users> usersList = usersService.selectUsersByRole(1);
        Date date1=DateUtil.strToDate(datetime1);
        Date date2=DateUtil.strToDate(datetime2);
        List<Users> datalist = new ArrayList<>();
        for (Users u:usersList){
            int i=u.getCreatetime().compareTo(date1);
            int j=u.getCreatetime().compareTo(date2);
            if (i>=0){
                if (j<=0){
                    datalist.add(u);
                }
            }
        }
        System.out.println();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("jsp/content_oem");
        modelAndView.addObject("datalist", datalist);
        modelAndView.addObject("datetime1", datetime1);
        modelAndView.addObject("datetime2", datetime2);
        // 设置页面进行跳转
        return modelAndView;
    }
}
